using System;
using System.Web.UI;
using SharpPieces.Web.Controls;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace SharpPieces.DemoApp
{

    /// <summary>
    /// The Dynamic Image Effects Extended page.
    /// </summary>
    public partial class DynamicImgEffectsExt : Page
    {

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs"></see> 
        /// object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            this.DynamicImage2.RegisterCustomTransformationType<InvertColorsImageTransformation>(null);
            this.DynamicImage3.RegisterCustomTransformationType<FourSplitTransformation>(null);
        }
    }

    /// <summary>
    /// Represents an image transformation by invert colors.
    /// </summary>
    public class InvertColorsImageTransformation : IImageTransformation
    {

        #region IImageTransformation Members

        /// <summary>
        /// Applies the specified image transformation.
        /// </summary>
        /// <param name="image">The image to be transfored.</param>
        /// <param name="parameters">The transformation params.</param>
        public void Apply(ref System.Drawing.Image image, NameValueCollection parameters)
        {
            using (Image imageCopy = (Image)image.Clone())
            using (Graphics g = Graphics.FromImage(image))
            using (ImageAttributes ia = new ImageAttributes())
            {
                ColorMatrix cm = new ColorMatrix(new float[][] {
                    new float[] { -1f,   0,   0,  0,  0 },
                    new float[] {   0,   0, -1f,  0,  0 },
                    new float[] {   0,   0,   0, 1f,  0 },
                    new float[] {   0,   0,   0,  0, 1f }
                    });
                ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Default);

                g.DrawImage(imageCopy, new Rectangle(Point.Empty, imageCopy.Size), 0, 0, imageCopy.Width,
                    imageCopy.Height, GraphicsUnit.Pixel, ia);
            }
        }

        #endregion

    }

    public class FourSplitTransformation : IImageTransformation
    {

        #region IImageTransformation Members

        /// <summary>
        /// Applies the specified image transformation.
        /// </summary>
        /// <param name="image">The image to be transfored.</param>
        /// <param name="parameters">The transformation params.</param>
        public void Apply(ref System.Drawing.Image image, NameValueCollection parameters)
        {
            Image newImage = new Bitmap(new Bitmap(image, image.Size + new Size(20, 20)));
            using (Graphics newG = Graphics.FromImage(newImage))
            {
                newG.Clear(Color.Transparent);

                SizeF sizeToCopy = new SizeF(image.Width / 2,image.Height / 2);

                newG.DrawImage(image, new RectangleF(Point.Empty, sizeToCopy), new RectangleF(Point.Empty, sizeToCopy), GraphicsUnit.Pixel);
                newG.DrawImage(image, new RectangleF(new Point(image.Width / 2 + 20, 0), sizeToCopy), new RectangleF(new Point(image.Width / 2, 0), sizeToCopy), GraphicsUnit.Pixel);
                newG.DrawImage(image, new RectangleF(new Point(0, image.Height / 2 + 20), sizeToCopy), new RectangleF(new Point(0, image.Height / 2), sizeToCopy), GraphicsUnit.Pixel);
                newG.DrawImage(image, new RectangleF(new Point(image.Width / 2 + 20, image.Height / 2 + 20), sizeToCopy), new RectangleF(new Point(image.Width / 2, image.Height / 2), sizeToCopy), GraphicsUnit.Pixel); 
            }

            image = newImage;
        }

        #endregion

    }

}
